<?php
/**
 * 牌局人数统计（去重）
 * @author chenjinle
 * @date 2017/6/20
 * php ../script.php request/PaijuNumHandler
 */

class PaijuNumHandler extends ScriptBase 
{
	/**
	 * 逻辑代码
	 */
	public function logic()
	{
		set_time_limit(0);
		$mysql = $this->mysql('logs');
		$redis = $this->redis('back_log');
		$num = 0;
		$cnt = 0;
		$cacheKey = 'PAIJU_UIDS';
		$date = '20170612';
		$start = strtotime("{$date}080000");
		$end = strtotime("{$date}105959");
		$start = date('Y-m-d H:i:s',$start);
		$end = date('Y-m-d H:i:s',$end);
		$table = "log_paiju_{$date}";
		$sql = "select uids,juShu from {$table} where time between {$start} and {$end}";
		// $sql = "select uids,juShu from {$table} where 1";
		if (!$mysql->query($sql)) {
			die('mysql query error');
		}
		while ($row = $mysql->fetch_assoc()) {
			if (empty($row['uids'])) continue;
			$uids = json_decode($row['uids'], true);
			foreach ($uids as $uid) {
				$redis->sAdd($cacheKey, $uid);
			}
			$cnt++;
			$num += (int)$row['juShu'];
		}
		
		echo sprintf("%s,%s,%s\n", $redis->sSize($cacheKey), $cnt, $num);
		// var_dump($redis->sMembers($cacheKey));
		echo "done";
		$redis->delete($cacheKey);
	}
}